package com.uum.admin.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uum.admin.pojo.entity.SysMenu;
import com.uum.admin.service.SysMenuService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.web.exception.BizException;
import com.uum.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "菜单接口")
@RestController
@RequestMapping("/api/v1/menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;


    @Operation(description = "查询菜单列表", businessType = BusinessType.QUERY, isSaveResponseData = false)
    @ApiOperation(value = "菜单列表")
    @GetMapping("list")
    public Result listMenus(@RequestParam(required = false) String name) {
        List<SysMenu> menusList = sysMenuService.listMenuByName(name);
        return Result.success(menusList);
    }

    @ApiOperation(value = "菜单列表(选择器)")
    @GetMapping("select")
    public Result selectMenu() {
        List<SysMenu> menuList = sysMenuService.listMenu();
        //添加顶级菜单
        SysMenu root = new SysMenu();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setChildren(menuList);

        List<SysMenu> list = new ArrayList<>();
        list.add(root);
        return Result.success(list);
    }


    @ApiOperation("菜单详情")
    @GetMapping("detail")
    public Result menuDetail(@RequestParam("id") Long id) {
        //根据Id查出菜单
        SysMenu menu = sysMenuService.getById(id);
        if (menu == null) {
            Result.failed("没有该菜单");
        }
        //为非根目录赋值parentName
        if (menu.getParentId() > 0) {
            SysMenu parentName = sysMenuService.getOne(new QueryWrapper<SysMenu>().eq("menu_id", menu.getParentId()));
            menu.setParentName(parentName.getName());
        }
        return Result.success(menu);
    }


    @Operation(description = "添加菜单", businessType = BusinessType.INSERT)
    @ApiOperation("添加菜单")
    @PostMapping("add")
    public Result addMenu(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);
        boolean result = sysMenuService.save(menu);
        return Result.judge(result);
    }

    @Operation(description = "修改菜单", businessType = BusinessType.UPDATE)
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result updateMenu(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);
        boolean result = sysMenuService.updateById(menu);
        return Result.judge(result);
    }

    @Operation(description = "删除菜单及子菜单",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除菜单(及子菜单)")
    @DeleteMapping("deleteMenuTree")
    public Result deleteMenuTree(@RequestParam("id") Long id) {
        boolean result = sysMenuService.delMenuTreeById(id);
        return Result.judge(result);
    }


    @Operation(description = "删除子菜单",businessType = BusinessType.DELETE)
    @ApiOperation("删除菜单")
    @DeleteMapping("deleteMenu")
    public Result deleteMenu(@RequestParam("id") Long id) {
        //判断是否含有children
        List<SysMenu> list = sysMenuService.list(new QueryWrapper<SysMenu>().eq("parent_id", id));
        if (list.size() > 0) {
            return Result.failed("请先删除子菜单");
        }
        boolean result = sysMenuService.removeById(id);
        return Result.judge(result);
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenu menu) {
        if (StrUtil.isBlank(menu.getName())) {
            throw new BizException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            throw new BizException("上级菜单不能为空");
        }

        //菜单
        if (menu.getType() == 1) {
            if (StrUtil.isBlank(menu.getUrl())) {
                throw new BizException("菜单URL不能为空");
            }
        }

        //上级菜单类型
        int parentType = 0;
        if (menu.getParentId() != 0) {
            SysMenu parentMenu = sysMenuService.getById(menu.getParentId());
            parentType = parentMenu.getType();
        }

        //目录、菜单
        if (menu.getType() == 0 || menu.getType() == 1) {
            if (parentType != 0) {
                throw new BizException("上级菜单只能为目录类型");
            }
        }
    }
}

